from django.shortcuts import render
from django.shortcuts import render_to_response
from django.template import RequestContext
#from django.contrib.auth import authenticate, login
#from django.contrib import auth

from django.http import HttpResponseRedirect
from django.core.context_processors import csrf

from login.models import client
from login.models import Hotels
from login.models import Review

# must import  loginForm class
from login.models import loginForm
from login.models import searchForm
from login.models import reviewForm

# import query class
from django.db.models import Q


##############################################################################################################


def index(request):

    #obj_form = loginForm()

    #context = {'obj_form': obj_form}

    return render(request,'login/index.html')




def search_hotel(request):

    hotel_info = Hotels.objects.all()

    # must in a dictionary
    hotel_data ={'hotel_detail':hotel_info}
    # to dispaly data

    #return render_to_response(context_intance=RequestContext(request),'login/search_hotel.html',hotel_data)
    # using render to repaint html page

    return render(request,'login/search_hotel.html',hotel_data)


def gallery(request):

    hotel_id = request.GET['hotel_id']


    # must in a dictionary
    hotel_data_id = {'hotel_id':hotel_id}

    #hotel_id =request.GET[hotel_id]
    #return render(request,'login/gallery.html')

    return render(request,'login/gallery.html',hotel_data_id)




def reviews(request):
    #from from url
    hotel_id = request.GET['hotel_id']

    #hotel_id = 1

    #hotel_info = Hotels.objects.all()
    #user_haslocations = has_location.objects.filter(user_has=user)

    hotel_info = Review.objects.filter(myreview_id_id = hotel_id)

    # must in a dictionary
    hotel_data ={'hotel_detail':hotel_info,'hotel_id':hotel_id}

    # to dispaly data
    # using render to repaint html page

    return render(request,'login/reviews.html',hotel_data)


    #return render(request,'login/reviews.html')


def new_review(request):

    #from from url
    hotel_id = request.GET['hotel_id']

    obj_form = reviewForm()

    context = {'obj_form': obj_form,'hotel_id':hotel_id}

    return render(request,'login/new_review.html',context)




def add_review(request):


    if request.method == 'POST':

        #dont make this###################
        #myreview_user_post = request.POST['myreview_user']
        #myreview_date_post = request.POST['myreview_date']
        #form = {'myreview_user':myreview_user_post}

        form = reviewForm(request.POST)

        if form.is_valid():

            form.save()
            return HttpResponseRedirect('/login/booking')


    args = {}
    args.update(csrf(request))

    args['form'] = reviewForm()
    print args

    return render(request,'/login/new_review.html',args)

##################################################################################################

def booking(request):


        #from from url
        #hotel_id = request.GET['hotel_id']


        obj_form = searchForm()
        context = {'obj_form': obj_form}

        #context = {'obj_form': obj_form,'hotel_id':hotel_id}
        #return render(request,'login/booking.html',context)

        return render(request,'login/booking.html',context)


def check_ava(request):

    if request.method == 'POST':

        hotel_name_post = request.POST['hotel_name']
        country_post = request.POST['country']
        busy_start_post = request.POST['busy_start']
        busy_end_post = request.POST['busy_end']
        room_type_post = request.POST['room_type']

        '''
        Poll.objects.get(
            Q(question__startswith='Who'),
            Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6))
        )
        SELECT * from Hotels
        WHERE hotel_name LIKE '' AND country LIKE '' AND room_type LIKE ''
        AND (busy_start != '2005-05-02' AND busy_end != '2005-05-06')


        Entry.objects.filter(pub_date__range=(start_date, end_date))
        '''

        hotel_info = Hotels.objects.filter(

            Q(hotel_name=hotel_name_post),
            Q(country=country_post),
            Q(room_type=room_type_post),

            ~Q(busy_start=busy_start_post) & ~Q(busy_end=busy_end_post) &

            ~Q(busy_start__range=(busy_start_post,busy_end_post)) &

            ~Q(busy_end__range=(busy_start_post,busy_end_post))

        )

        # must in a dictionary
        hotel_data ={'hotel_info':hotel_info}

        if not hotel_info:
            return render(request,'login/no_result.html')
            #HttpResponseRedirect('/login/no_result')

        else:
            return render(request,'login/list_rooms.html',hotel_data)





def no_result(request):

    return render(request,'login/no_result.html')



'''
def add_review(request):

    #from from url
    #hotel_id = request.GET['hotel_id']

    if request.method == 'POST':
        form = reviewForm(request.POST)

        if form.is_valid():

            form.save()
            return HttpResponseRedirect('/login/index')


    args = {}
    args.update(csrf(request))

    args['form'] = reviewForm()
    print args


    return render(request,'/login/new_review.html',args)

'''


"""
def login(request):
    c = {}
    c.update(csrf(request))
    return render_to_response('login.html',c)

def auth_view(request):
    username = request.POST.get('username','')
    password = request.POST.get('password','')
    user = auth.authenticate(username=username, password=password)

    if user is not None:

        login(request, user)
        return HttpResponseRedirect('/login/loggedin')
    else:
        return HttpResponseRedirect('/login/invalid')

def loggedin(request):
    return render_to_response('loggedin.html',{'full_name':request.user.username})

def invalid_login(request):
    return render_to_response('invalid_login.html')






########################################################################################

def signup(request):

    #to create object from loginform

    #obj_form = loginForm()

    #context = {'obj_form': obj_form}

    context = RequestContext(request)

    if request.method == 'POST':

        form = loginForm(request.POST)

        if form.is_valid():

            # Save the new category to the database.
            form.save(commit=True)
            return index(request)
        else:
            "Error"
    else:
        form = loginForm()

    return render_to_response('login/signup.html', {'form': form}, context)

###################################################################################################

def sign_in (request):

    context = RequestContext(request)

    if request.method == 'POST':

        form = loginForm(request.POST)

        if form.is_valid():

            all_users = client.objects.all()

            client.objects.filter(email=request.POST['email'], password=request.POST['password']).exists()

            for s in all_users:
                print s.email
                print s.password
                print s.name


    else:
        form = loginForm()

    return render_to_response('login/sign_in.html', {'form': form}, context)


########################################################
def login_user(request):
    state = "Please log in below..."

    username = password = ''

    if request.POST:
        username = client.objects.filter(email=request.POST.get('username'))
        password = client.objects.filter(password=request.POST.get('password'))

        #by me

        #client.objects.filter(email=request.POST['email'], password=request.POST['password']).exists()

        user = authenticate(username=username, password=password)

        if user is not None:
            if user.is_active:

                login(request, user)
                state = "You're successfully logged in!"
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

    return render_to_response('login/auth.html',{'state':state, 'username': username})


#######################################################################################################


def signup(request):

    #to create object from loginform

    obj_form = loginForm()

    context = {'obj_form': obj_form}

    if request.method == 'POST':

        #myclient = client.objects.get(title=offset)

        # Create a form instance from POST data.
        obj_form = loginForm(request.POST)

        if obj_form.is_valid():
            # Save a new Article object from the form's data.
            obj_form.save()

    return render_to_response('login/signup.html',context,context_instance=RequestContext(request))

            #return render(request,'login/signup.html', context)


def save_signup(request,offset):

    if request.method == 'POST':

        #myclient = client.objects.get(title=offset)

        # Create a form instance from POST data.
        form = loginForm(request.POST)
        if form.is_valid():
            # Save a new Article object from the form's data.
            form.save()

            #return redirect('/index/')

    #return render_to_response('login/signup.html',{'form':form},context_instance=RequestContext(request))

"""
